<?php
namespace App;
use Auth;
use Log;
class Utils
{
	private static $spiderSite= array(
        "TencentTraveler",
        "Baiduspider+",
        "BaiduGame",
        "Googlebot",
        "msnbot",
        "Sosospider+",
        "Sogou web spider",
        "ia_archiver",
        "Yahoo! Slurp",
        "YoudaoBot",
        "Yahoo Slurp",
        "MSNBot",
        "Java (Often spam bot)",
        "BaiDuSpider",
        "Voila",
        "Yandex bot",
        "BSpider",
        "twiceler",
        "Sogou Spider",
        "Speedy Spider",
        "Google AdSense",
        "Heritrix",
        "Python-urllib",
        "Alexa (IA Archiver)",
        "Ask",
        "Exabot",
        "Custo",
        "OutfoxBot/YodaoBot",
        "yacy",
        "SurveyBot",
        "legs",
        "lwp-trivial",
        "Nutch",
        "StackRambler",
        "The web archive (IA Archiver)",
        "Perl tool",
        "MJ12bot",
        "Netcraft",
        "MSIECrawler",
        "WGet tools",
        "larbin",
        "Fish search",
		"curl",
    );

    public static function tree($id) {
        if ($id != -1) {
            return '/u/'. Utils::id2url($id);
        }
        return '#';
    }
    public static function id2url($id)
    {
        $id = ($id + 9993) * 3;
        return dechex($id);
    }

    public static function url2id($url)
    {
        return hexdec($url) / 3 - 9993;
    }

    public static function score2step($s)
    {
        if ($s >= 5000) return 963;
        if ($s >= 2000) return 463;
        if ($s >= 1000) return 263;
        if ($s >= 800) return 163;
        if ($s >= 500) return 83;
        if ($s >= 200) return 33;
        if ($s >= 100) return 13;
        if ($s >= 10) return 3;
        return 0;
    }

    public static function log($request, $action)
    {
		$agent = $request->server('HTTP_USER_AGENT');
        foreach(Utils::$spiderSite as $val) {
            $str = strtolower($val);
            if (strpos($agent, $str) !== false) {
                return;
            }
        }

        $data = array();
        if (Auth::check()) {
            $data['userid'] = Auth::user()->id;
        } else $data['userid'] = '0';
        $data['referer'] = $request->server('HTTP_REFERER');
        $data['url'] = $request->server('REQUEST_URI');
        $data['ip'] = $request->getClientIp();
        $data['action'] = $action;
		$data['time'] = time();
        Log::info(json_encode($data));
    }
}

//echo Utils::id2url(25);
?>
